#!/bin/bash

# Specify ImageMagick
export MAGICK_HOME="$HOME/ImageMagick-7.0.10"
export PATH="$MAGICK_HOME/bin:$PATH"
export DYLD_LIBRARY_PATH="$MAGICK_HOME/lib/"

# create dir "clipboard_trans"
cd ~
if [ ! -d "Clipboard_trans"  ];then
  mkdir "Clipboard_trans"
fi
cd "Clipboard_trans"
mark=0

# read Clipboard
result=$(osascript -e 'clipboard info' | grep '«class PNGf»')
if [[ "$result" != "" ]]
then
	echo -e "1.save tiff"
	osascript -e "get the clipboard as «class PNGf»" | sed "s/«data PNGf//; s/»//" | xxd -r -p > x.tiff

	echo "2.convert to png"
	sips -s format png -s formatOptions default x.tiff --out x.png

    convert x.png \
    \( +clone  -alpha extract \
        -draw 'fill black polygon 0,0 0,5 5,0 fill white circle 5,5 5,0' \
        \( +clone -flip \) -compose Multiply -composite \
        \( +clone -flop \) -compose Multiply -composite \
    \) -alpha off -compose CopyOpacity -composite x_with_rounded_corners.png

	echo "3.copy png to clipboard"
	osascript -e 'set the clipboard to (read "x_with_rounded_corners.png" as «class PNGf»)'
	
	# exit shell
	exit
else
	echo "Clipboard not found tiff image."
fi

echo "Nothing to do!!!"